// stores/counter.js
import {
  defineStore
} from 'pinia'

import {
  login
} from '@/api/user.js'

import {
  setToken,
  getToken,
  setItem,
  getItem
} from '@/utils/auth'

export const useCounterStore = defineStore('counter', {
  state: () => {
    return {
      token: getToken(),
      counter: getItem('counter', 1), // 用来储存单条学历ID或
      employeeId: getItem('employeeId'), // 用户ID
      oldEmployeeId:getItem('oldEmployeeId'),  // 旧的用户id
      mobile: getItem('mobile'), // 手机号码
      isSubmit: getItem('isSubmit'),
      HireType:getItem('HireType'),
      includeList:[],
      isAttribution:getItem('isAttribution')
    }
  },
  // 也可以定义为
  // state: () => ({ count: 0 })
  actions: {
    login(from) {
      return new Promise((resolve, rejects) => {
        login({
          mobile: from.phone,
          messageCode: from.sms,
          code: from.DynamicVerificationCode
        }).then(res => {
          if (!res.success) return resolve(res)
          this.getToken(res.message.token)
          resolve(res)
        })
      })
    },
    setIncludeList(value){
      this.includeList = value
    },
    setOldEmployeeId(value){
      this.oldEmployeeId = value
      console.log(value)
      setItem('oldEmployeeId',value)
    },
    getToken(token) {
      setToken(token)
      this.token = token
    },
    getMobulde(value) {
      this.mobile = value
      setItem('mobile', value)
    },
    getHireType(value) {
      this.HireType = value
      setItem('HireType', value)
    },
    setHireType(value) {
      this.HireType = value
      setItem('HireType', value)
    },
    setEmployeeId(value) {
      this.employeeId = value
      setItem('employeeId', value)
    },
    setCounter(value) {
      const obj = {
        ...this.counter,
        ...value
      }
      this.counter = obj
      const a = JSON.stringify(obj)
      setItem('counter', a)
    },
    setIsSubmit(value) {
      this.isSubmit = value
      setItem('isSubmit',value)
    },
    defaultCounter() {
      this.counter = {}
      const a = JSON.stringify({})
      setItem('counter', a)
    },
    setIsAttribution(value){
      this.isAttribution = value
      setItem('isAttribution', value)
    }
  },
})
